import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    hidden: true
  },
  {
    path: '/ssologin',
    component: () => import('@/views/sso-login/index.vue'),
    hidden: true
  },
  {
    path: '/authorization',
    component: () => import('@/views/authorization/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/autherr',
    component: () => import('@/views/error-page/authErr'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/workingHours',
    children: [
      {
        path: 'workingHours',
        component: () => import('@/views/working-hours/index'),
        name: 'workingHours',
        meta: { title: '工时管理', icon: 'edit', affix: false }
      }
    ]
  },
  {
    path: '/robotTask',
    component: Layout,
    redirect: '/robotTask',
    meta: {
      icon: 'huizong2', affix: false,
      title: "任务告警",
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/robot-task/index.vue'),
        name: 'robotTask-index',
        meta: { title: '通用任务告警', icon: 'task', affix: false }
      },
      {
        path: 'webapi',
        component: () => import('@/views/robot-task-webapi/index.vue'),
        name: 'robotTaskWebapi-index',
        meta: { title: '网站&API告警', icon: 'task', affix: false }
      }
    ]
  },
  {
    path: '/accredit',
    component: Layout,
    redirect: '/accredit',
    meta: {
      icon: 'Accredit', affix: false,
      title: "统一授权管理",
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/accredit/index'),
        name: 'brand-index',
        meta: { title: '授权地址', icon: 'Accredit', affix: false}
      },
      {
        path: 'userlists',
        component: () => import('@/views/accredit-user/index'),
        name: 'brand-index',
        meta: { title: '授权账号', icon: 'Accredit', affix: false}
      },
      {
        path: 'sysrole',
        component: () => import('@/views/accredit/sysRole'),
        name: 'sys-role',
        meta: { title: '角色管理' },
        hidden: true
      }
    ]
  },
]

export const asyncRoutes = [
  {
    path: '/order',
    component: Layout,
    redirect: '/order',
    // alwaysShow: true,
    meta: { roles: ['admin', 'manager'] },
    children: [
      {
        path: 'index',
        component: () => import('@/views/order-form/index'),
        name: 'order-index',
        meta: { title: '订单管理', icon: 'excel', affix: false }
      },
      {
        path: 'details',
        component: () => import('@/views/order-form/details'),
        name: 'order-details',
        meta: { title: '订单详情' },
        hidden: true
      }
    ]
  },
  {
    path: '/project',
    component: Layout,
    redirect: '/project',
    meta: { roles: ['admin', 'manager'] },
    children: [
      {
        path: 'index',
        component: () => import('@/views/project/index'),
        name: 'project-index',
        meta: { title: '项目管理', icon: 'documentation', affix: false }
      },
      {
        path: 'details',
        component: () => import('@/views/project/details'),
        name: 'project-details',
        meta: { title: '项目详情' },
        hidden: true
      }
    ]
  },
  {
    path: '/brand',
    component: Layout,
    redirect: '/brand/index',
    meta: { roles: ['admin', 'manager'] },
    children: [
      {
        path: '/brand/index',
        component: () => import('@/views/admin/brand/index'),
        name: 'brand-index',
        meta: { title: '品牌管理', icon: 'pinpai', affix: false, roles: ['admin', 'manager'] }
      },
    ]
  },
  {
    path: '/working/statistics',
    component: Layout,
    redirect: '/working/statistics/index',
    meta: {
      icon: 'huizong2', affix: false,
      roles: ['admin', 'manager'],
      title: "工时统计",
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/admin/collect/index'),
        name: 'collect-index',
        meta: { title: '工时汇总', icon: 'huizong2', affix: false, roles: ['admin', 'manager'] }
      },
      {
        path: 'department',
        component: () => import('@/views/admin/collect/department'),
        name: 'collect-department',
        meta: { title: '部门筛选', icon: 'huizong2', affix: false, roles: ['admin', 'manager'] }
      },
    ]
  },
  {
    path: '/collect',
    component: Layout,
    redirect: '/collect/index',
    meta: {
      icon: 'huizong2', affix: false,
      roles: ['admin'],
      title: "系统管理",
    },
    children: [
      {
        path: '/user/index',
        component: () => import('@/views/admin/user/index'),
        name: 'user-index',
        meta: { title: '用户管理', icon: 'user', affix: false, roles: ['admin'] }
      },
      {
        path: '/configuration/index',
        component: () => import('@/views/admin/configuration/index'),
        name: 'configuration-index',
        meta: { title: '数据字典', icon: 'peizhi3', affix: false, roles: ['admin'] }
      },
      {
        path: '/configuration/details',
        component: () => import('@/views/admin/configuration/details'),
        name: 'configuration-details',
        meta: { title: '配置详情', roles: ['admin'] },
        hidden: true
      },
      {
        path: '/sys/user/role',
        component: () => import('@/views/admin/user-role/index'),
        name: 'user-role',
        meta: { title: '角色管理', icon: 'peizhi3', affix: false, roles: ['admin'] }
      }
    ]
  }
]
const createRouter = () => new Router({
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }), // 使用前端路由，当切换到新路由时，想要页面滚到顶部，或者是保持原先的滚动位置，就像重新加载页面那样。       vue-router 能做到，而且更好，它让你可以自定义路由切换时页面如何滚动。
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
